package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private ItemCatMapper itemCatMapper;

    public Map<Integer, List<ItemCat>> getMap() {
        Map<Integer, List<ItemCat>> map = new HashMap<>();
        //查询所有的数据库记录
        List<ItemCat> list = itemCatMapper.selectList(null);
        //1.遍历数据
        for (ItemCat itemCat : list) {
            int parentId = itemCat.getParentId();
            if (map.containsKey(parentId)) {
                //表示数据存在,将自己追加
                map.get(parentId).add(itemCat);
            } else {
                //key不存在, 定义list集合,将自己作为第一个元素追加
                List<ItemCat> childrenList = new ArrayList<>();
                childrenList.add(itemCat);
                //将数据保存到map集合中
                map.put(parentId, childrenList);
            }
        }
        return map;
    }

    public List<ItemCat> getTwoList(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> onelist = map.get(0);
        for (ItemCat oneItemCat : onelist) {
            int parentId = oneItemCat.getId();
            List<ItemCat> towlist = map.get(parentId);
            oneItemCat.setChildren(towlist);
        }
        return onelist;
    }

    public List<ItemCat> getThreeList(Map<Integer, List<com.jt.pojo.ItemCat>> map) {
        List<ItemCat> oneList = getTwoList(map);
        for (ItemCat oneItemCat : oneList) {
            List<ItemCat> twoList = oneItemCat.getChildren();
            if (twoList == null || twoList.size() == 0) {
                //跳过本地循环，进入下一次循环
                continue;
            }
            for (ItemCat twoItemCat : twoList) {
                int parentId = twoItemCat.getId();
                List<ItemCat> threeList = map.get(parentId);
                twoItemCat.setChildren(threeList);
            }
        }
        return oneList;
    }

    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        Map<Integer, List<ItemCat>> map = getMap();
        //返回1级
        if (level == 1) {
            return getMap().get(0);
        }
        //返回1-2级
        if (level == 2) {
            return getTwoList(map);
        }
        //返回1-2-3级
        return getThreeList(map);
    }

    @Override
    @Transactional
    public void saveItemCat(ItemCat itemCat) {
        Date date = new Date();
        itemCat.setStatus(true)
                .setUpdated(date)
                .setCreated(date);
        itemCatMapper.insert(itemCat);
    }

    @Override
    public void deleteItemCat(ItemCat itemCat) {
        //如果删除3级
        if (itemCat.getLevel() == 3) {
            Integer id = itemCat.getId();
            itemCatMapper.deleteById(id);
            return;
        }
        //如果删除2级
        if (itemCat.getLevel() == 2) {
            Integer id = itemCat.getId();
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper();
            queryWrapper.eq("parent_id", id);
            itemCatMapper.delete(queryWrapper);
            return;
        }
        //如果删除1级

        //查询二级的id集合,
        Integer id = itemCat.getId();
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id", id);
        List twolist = itemCatMapper.selectObjs(queryWrapper);//二级的id集合
        if (twolist.size() > 0) {//判断是否有二级数据
            queryWrapper.clear();
            queryWrapper.in("parent_id", id);
            itemCatMapper.delete(queryWrapper);//根据条件parent_id=id删除三级
            twolist.add(id);
            itemCatMapper.deleteBatchIds(twolist);//删除一级和二级
        } else {
            itemCatMapper.deleteById(id);
        }
    }

    @Override
    @Transactional
    public void changeStatus(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

    @Override
    @Transactional
    public void updateItemCat(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

}
